package com.larissa.android.quizdemo;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.larissa.android.quizdemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG="MainActivity";
    private static final String ANSWER_KEY="ANSWER_KEY";
    private static final String RESULT_KEY="RESULT_KEY";
    private ActivityMainBinding mActivityMainBinding;
    private QuizViewModel mQuizViewModel;

    private ActivityResultLauncher mHelpLauncher=
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
                // 实现ActivityResult的回调接口ActivityResultCallback
                // 当resultCode为RESULT_OK时，从ActivityResult中获取用户是否查看答案的数据，并写入QuizViewModel中
                if(result.getResultCode()==Activity.RESULT_OK)
                    mQuizViewModel
                            .setHelpKey(result.getData()==null? false:result.getData().getBooleanExtra(RESULT_KEY,false));
                // 从QuizViewModel中获取数据，如果用户在HelpActivity页面查看了数据，则此题无效
                if(mQuizViewModel.getHelpKey())
                    Toast.makeText(this,"This question is invalid",Toast.LENGTH_LONG).show();
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate() called");
        mActivityMainBinding=ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mActivityMainBinding.getRoot());

        mQuizViewModel= new ViewModelProvider(this).get(QuizViewModel.class);

        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle(TAG);

        mActivityMainBinding.btnFalse.setOnClickListener(this);
        mActivityMainBinding.btnTrue.setOnClickListener(this);
        mActivityMainBinding.btnHelp.setOnClickListener(this);

        showQuestion();
    }

    @Override
    public void onClick(View view){
        if(view.getId()==R.id.btn_true){
            if(mQuizViewModel.isCurrentQuestionAnswer() && !mQuizViewModel.getHelpKey()){
                mQuizViewModel.mCount++;
            }
            updateQuestion();
        }else if(view.getId()==R.id.btn_false){
            if(!mQuizViewModel.isCurrentQuestionAnswer() && !mQuizViewModel.getHelpKey())
                mQuizViewModel.mCount++;
            updateQuestion();
        }else if(view.getId()==R.id.btn_help){
            Intent intent=new Intent(MainActivity.this,HelpActivity.class);
            intent.putExtra(ANSWER_KEY,mQuizViewModel.isCurrentQuestionAnswer());
//            startActivity(intent);
//            startActivityForResult(intent,0);
            mHelpLauncher.launch(intent);
        }
    }

    private void updateQuestion(){

        mQuizViewModel.moveToNext();
        mQuizViewModel.setHelpKey(false);
        showQuestion();
    }


    private void showQuestion(){

        int mCurrentIdx=mQuizViewModel.mCurrentIdx;
        int mQuestionId=mQuizViewModel.getCurrentQuestionText();
        if(mCurrentIdx==-1){
            mActivityMainBinding.btnTrue.setEnabled(false);
            mActivityMainBinding.btnFalse.setEnabled(false);
            mActivityMainBinding.btnHelp.setEnabled(false);
            String toastText=String.format("%s questions are right.",mQuizViewModel.mCount);
            Toast.makeText(MainActivity.this,toastText,Toast.LENGTH_SHORT).show();
            mActivityMainBinding.txQuestion.setText(mQuestionId);
        }
        else{
            mActivityMainBinding.txQuestion.setText(mQuestionId);
        }
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }
}